package wiki.base;

import java.lang.reflect.Field;

import il.ac.technion.wiki.impl.DataBase;
import il.ac.technion.wiki.impl.WikiPageStandard;

/**
 * Dependency-injected variant of the {@link il.ac.technion.wiki.impl.WikiPageStandard WikiPageStandard}
 * class. This behaves exactly like its parent, with the exception that it allows the user to explicitly
 * specify at construction time a different DataBase instance to use for storage - allowing him to use
 * databases other than the default.
 *
 */
public class WikiPageDI extends WikiPageStandard {

	/**
	 * Construct the wiki page with default database implementation (same as constructing a
	 * {@link il.ac.technion.wiki.impl.WikiPageStandard WikiPageStandard} directly).
	 */
	public WikiPageDI() {
		super();
	}

	/**
	 * Construct the wiki page using the specified database for storage.
	 * @param db	The database instance to use for storage.
	 */
	public WikiPageDI(DataBase db) {
		super();
		
		try {
			// we use reflection to modify the instance's private "db" field
			Field f = WikiPageStandard.class.getDeclaredField("db");
			f.setAccessible(true);
			f.set(this, db);
			f.setAccessible(false);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
}
